package com.qf.dblib.dao.impl;


import com.qf.dblib.dao.entity.LocationBean;

import org.litepal.LitePal;

/**
 * 定位记录控制类
 * 2020/4/1 17:37
 */
public class LocationDao
{
    private static volatile LocationDao instance = null;
    private LocationBean bean;

    private LocationDao()
    {
        bean = LitePal.findLast(LocationBean.class);
    }

    public static LocationDao getInstance()
    {
        if (instance == null)
        {
            synchronized (LocationDao.class)
            {
                if (instance == null)
                {
                    instance = new LocationDao();
                }
            }
        }
        return instance;
    }


    /**
     * 保存定位信息
     */
    public boolean saveLocation(LocationBean location)
    {
        if (location == null)
            return false;
        if (bean == null)
            bean = new LocationBean();
        else {
            bean.setLng(location.getLng());
            bean.setLat(location.getLat());
            bean.setAdCode(location.getAdCode());
            bean.setProvince(location.getProvince());
            bean.setCity(location.getCity());
            bean.setCountry(location.getCountry());
        }

        return bean.saveOrUpdate();
    }

    /**
     * 查询定位信息
     */
    public LocationBean getLocationBean()
    {
        return bean;
    }

    /**
     * 清空定位信息
     */
    public boolean clearLocation()
    {
        instance = null;
        bean = null;
        return LitePal.deleteAll(LocationBean.class) > 0;
    }

}
